package com.hz.monitoring.module.hardware.service.sensor_tenant_type;

import com.hz.monitoring.framework.common.pojo.PageResult;
import com.hz.monitoring.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hz.monitoring.module.hardware.controller.admin.sensor_tenant_type.vo.*;
import com.hz.monitoring.module.hardware.convert.sensor_tenant_type.SensorTenantTypeConvert;
import com.hz.monitoring.module.hardware.dal.dataobject.sensor_sys_type.SensorSysTypeDO;
import com.hz.monitoring.module.hardware.dal.dataobject.sensor_tenant_type.SensorTenantTypeDO;
import com.hz.monitoring.module.hardware.dal.mysql.sensor_sys_type.SensorSysTypeMapper;
import com.hz.monitoring.module.hardware.dal.mysql.sensor_tenant_type.SensorTenantTypeMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.hz.monitoring.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.hz.monitoring.module.hardware.enums.ErrorCodeConstants.SENSOR_TENANT_TYPE_NOT_EXISTS;

/**
 * 传感器租户分类 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class SensorTenantTypeServiceImpl implements SensorTenantTypeService {

    @Resource
    private SensorTenantTypeMapper sensorTenantTypeMapper;

    @Resource
    private SensorSysTypeMapper sensorSysTypeMapper;


    @Override
    public Long createSensorTenantType(SensorTenantTypeCreateReqVO createReqVO) {
        // 插入
        SensorTenantTypeDO sensorTenantType = SensorTenantTypeConvert.INSTANCE.convert(createReqVO);
        sensorTenantTypeMapper.insert(sensorTenantType);
        // 返回
        return sensorTenantType.getId();
    }

    @Override
    public Boolean createBatchSensorTenantType(List<SensorTenantTypeCreateReqVO> createReqVO) {
        return sensorTenantTypeMapper.createBatchSensorTenantType(createReqVO);
    }

    @Override
    public void updateSensorTenantType(SensorTenantTypeUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateSensorTenantTypeExists(updateReqVO.getId());
        // 更新
        SensorTenantTypeDO updateObj = SensorTenantTypeConvert.INSTANCE.convert(updateReqVO);
        sensorTenantTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteSensorTenantType(Long id) {
        // 校验存在
        this.validateSensorTenantTypeExists(id);
        // 删除
        sensorTenantTypeMapper.deleteById(id);
    }

    private void validateSensorTenantTypeExists(Long id) {
        if (sensorTenantTypeMapper.selectById(id) == null) {
            throw exception(SENSOR_TENANT_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public SensorTenantTypeDO getSensorTenantType(Long id) {
        return sensorTenantTypeMapper.selectById(id);
    }

    @Override
    public List<SensorTenantTypeDO> getSensorTenantTypeList(SensorTenantTypeListReqVO listReqVO) {
        return sensorTenantTypeMapper.selectList(listReqVO);
    }

    @Override
    public List<SensorSysTypeDO> getSensorTenantTypeInfoList(SensorTenantTypeListReqVO cmd) {
        List<SensorTenantTypeDO> list=sensorTenantTypeMapper.selectList(cmd);
        List<String> models=list.stream().map(SensorTenantTypeDO::getModel).collect(Collectors.toList());
        return sensorSysTypeMapper.selectList(new LambdaQueryWrapperX<SensorSysTypeDO>()
                .eqIfPresent(SensorSysTypeDO::getMcuModel, cmd.getMcuModel())
                .inIfPresent(SensorSysTypeDO::getModel, models)
                .orderByDesc(SensorSysTypeDO::getId));
    }

    @Override
    public PageResult<SensorTenantTypeDO> getSensorTenantTypePage(SensorTenantTypePageReqVO pageReqVO) {
        return sensorTenantTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SensorTenantTypeDO> getSensorTenantTypeList(SensorTenantTypeExportReqVO exportReqVO) {
        return sensorTenantTypeMapper.selectList(exportReqVO);
    }

}
